﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Edu;

namespace SysManage.Module.Edu
{
    public partial class PatyEduSpecialEdit : BLL.PageBase
    {
        #region ViewPower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "PartyEduSpecialAdd";
            }
        }

        public string Category
        {
            get { return Request.QueryString["Category"] ?? ""; }
        }
        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
            string id = GetQueryIntValue("id");
            if (id !="")
            {
                PartyEduSpecial current = DB.PartyEduSpecials.Find(id);
                if (current == null)
                {
                    // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                    Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                    return;
                }
                tbxSpecialFlag.Readonly = true;
                tbxSortIndex.Text = current.OrderNo.ToString();
                tbxSpecialFlag.Text = current.SpecialFlag;
                tbxSpecialName.Text = current.SpecialName;
                tbxStudyDescription.Text = current.StudyDescription;
                cbxEnabled.Checked = current.Enabled;

            }
        }

        #endregion

        #region Events

        private void SaveJobTitle()
        {
            PartyEduSpecial item;
            string id = GetQueryIntValue("id");
            if (id !="")
            {
                //更新
               item = DB.PartyEduSpecials.Find(id);
                if (item == null)
                {
                    Alert.Show("参数错误！", String.Empty);
                    return;
                }
                item.ModifyTime = DateTime.Now;
                item.ModifyBy = CurrentUser.ID;
            }
            else
            {
                var current = DB.SystemParameters.Find(this.tbxSpecialFlag.Text.Trim());
                if (current != null)
                {
                    Alert.Show("模块标识已存在，请重新输入！", String.Empty);
                    return;
                }
                item = new PartyEduSpecial();
                item.ID = StringHelper.NewID;
                item.CreateTime = DateTime.Now;
                item.CreateBy = CurrentUser.ID;
                item.ModifyTime = DateTime.Now;
                item.ModifyBy = CurrentUser.ID;
                if (Category == "MassLine")
                    item.Category = "QuestionnaireMassLine";
                else item.Category = "Ordinary";//普通专题
                DB.PartyEduSpecials.Add(item);

            }
            item.OrderNo = Convert.ToInt32(tbxSortIndex.Text);
            item.SpecialFlag = tbxSpecialFlag.Text.Trim();
            item.SpecialName = tbxSpecialName.Text.Trim();
            item.StudyDescription = tbxStudyDescription.Text.Trim();
          
            item.Enabled = cbxEnabled.Checked;
            DB.SaveChanges();
        }

        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            SaveJobTitle();

            //Alert.Show("添加成功！", String.Empty, ActiveWindow.GetHidePostBackReference());
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion

    }
}
